#include <stdio.h>
#include "string.h"
#include "iot_debug.h"
#include "iot_uart.h"
#include "iot_os.h"

#include "shell.h"
#include "porting.h"

typedef enum
{
  UART_RECV_MSG = 1,
}TASK_MSG_ID;

typedef struct
{
  TASK_MSG_ID id;
  UINT32 len;
  void *param;
}TASK_MSG;

Shell shell;
HANDLE main_thread;
char shell_buffer[512];

void uart_msg_send(HANDLE hTask, TASK_MSG_ID id, void *param, UINT32 len)
{
  TASK_MSG *msg = NULL;

  msg = (TASK_MSG *)iot_os_malloc(sizeof(TASK_MSG));
  msg->id = id;
  msg->param = param;
  msg->len = len;

  iot_os_send_message(hTask, msg);
}

//中断方式读串口1数据
//注: 中断中有复杂的逻辑,要发送消息到task中处理
void uart_recv_handle(T_AMOPENAT_UART_MESSAGE* evt)
{
  char *recv_buff = NULL;
  int32 recv_len;
  int32 dataLen = evt->param.dataLen;
  if(!dataLen) {
    return;
  }

  recv_buff = iot_os_malloc(dataLen);
  if(recv_buff == NULL) {
    iot_debug_print("uart_recv_handle_0 recv_buff malloc fail %d", dataLen);
    return;
  }	
  switch(evt->evtId) {
    case OPENAT_DRV_EVT_UART_RX_DATA_IND:
      recv_len = iot_uart_read(SHELL_UART_PORT, (UINT8*)recv_buff, dataLen , UART_RECV_TIMEOUT);
      iot_debug_print("uart_recv_handle_1:recv_len %d", recv_len);
      iot_debug_print("uart_recv_handle_1:recv_len %d", recv_len);
      uart_msg_send(main_thread, UART_RECV_MSG, recv_buff, recv_len);

      break;
    case OPENAT_DRV_EVT_UART_TX_DONE_IND:
      iot_debug_print("uart_recv_handle_2 OPENAT_DRV_EVT_UART_TX_DONE_IND");
      break;
    default:
      iot_debug_print("unknown event %d", evt->evtId);
      break;
  }
}

void uart_open(void)
{
  BOOL err;
  T_AMOPENAT_UART_PARAM uartCfg;
  
  memset(&uartCfg, 0, sizeof(T_AMOPENAT_UART_PARAM));
  uartCfg.baud = OPENAT_UART_BAUD_115200; //波特率
  uartCfg.dataBits = 8;   //数据位
  uartCfg.stopBits = 1; // 停止位
  uartCfg.parity = OPENAT_UART_NO_PARITY; // 无校验
  uartCfg.flowControl = OPENAT_UART_FLOWCONTROL_NONE; //无流控
  uartCfg.txDoneReport = TRUE; // 设置TURE可以在回调函数中收到OPENAT_DRV_EVT_UART_TX_DONE_IND
  uartCfg.uartMsgHande = uart_recv_handle; //回调函数

  // 配置uart1 使用中断方式读数据
  err = iot_uart_open(SHELL_UART_PORT, &uartCfg);
  iot_debug_print("[uart] uart_open_2 err: %d", err);
}

void shell_writer(const char word)
{
  iot_uart_write(SHELL_UART_PORT, (UINT8*)(&word), 1);
}

void main_entry(void* params)
{
  uart_open();
  shell.read = NULL;
  shell.write = shell_writer;
  shellInit(&shell, shell_buffer, sizeof(shell_buffer));

	TASK_MSG *msg = NULL;
  while(1) {
    iot_os_wait_message(main_thread, (PVOID*)&msg);
    switch(msg->id) {
      case UART_RECV_MSG:    
        iot_debug_print("[uart] uart_task_main_1 recv_len %s", msg->param);
        char* rcvData = msg->param;
        for(int i=0; i!=msg->len; ++i) {
          shellHandler(&shell, rcvData[i]);
        }
        break;
      default:
        iot_debug_print('[uart] unknown thread message. %d', msg->id);
        break;
    }

    if(msg) {
      if(msg->param) {
        iot_os_free(msg->param);
        msg->param = NULL;
      }
      iot_os_free(msg);
      msg = NULL;
      iot_debug_print("[uart] uart_task_main_2 uart free");
    }
  }
}

int appimg_enter(void *param)
{
  iot_debug_print("[hello]appimg_enter");
  main_thread = iot_os_create_task(main_entry, NULL, 64 * 1000, 1, OPENAT_OS_CREATE_DEFAULT, "uart_task");
  return 0;
}

void appimg_exit(void)
{
  iot_debug_print("[hello]appimg_exit");
}
